//
//  BADGetIphone.m
//  BAD
//
//  Created by Can on 2018/5/24.
//  Copyright © 2018年 hwd. All rights reserved.
//

#import "BADGetIphone.h"
#import <ifaddrs.h>
#import <arpa/inet.h>
#import <sys/utsname.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <net/if.h>
#include <ifaddrs.h>
#import <dlfcn.h>


@implementation BADGetIphone

+(NSString *)getIPAddress {
    NSString *address = @"error";
    struct ifaddrs *interfaces = NULL;
    struct ifaddrs *temp_addr = NULL;
    int success = 0;
    // 检索当前接口,在成功时,返回0
    success = getifaddrs(&interfaces);
    if (success == 0) {
        // 循环链表的接口
        temp_addr = interfaces;
        while(temp_addr != NULL) {
            if(temp_addr->ifa_addr->sa_family == AF_INET) {
                // 检查接口是否en0 wifi连接在iPhone上
                if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
                    // 得到NSString从C字符串
                    address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
                }
            }
            temp_addr = temp_addr->ifa_next;
        }
    }
    // 释放内存
    freeifaddrs(interfaces);
    return address;
}


+ (NSString *)getDeviceVersion{
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    //iPhone
    if ([deviceString isEqualToString:@"iPhone1,1"])    return @"iPhone 1G";
    else if ([deviceString isEqualToString:@"iPhone1,2"])    return @"iPhone 3G";
    else if ([deviceString isEqualToString:@"iPhone2,1"])    return @"iPhone 3GS";
    else if ([deviceString isEqualToString:@"iPhone3,1"])    return @"iPhone 4";
    else if ([deviceString isEqualToString:@"iPhone3,2"])    return @"iPhone 4";
    else if ([deviceString isEqualToString:@"iPhone4,1"])    return @"iPhone 4S";
    else if ([deviceString isEqualToString:@"iPhone5,1"])    return @"iPhone 5";
    else if ([deviceString isEqualToString:@"iPhone5,2"])    return @"iPhone 5";
    else if ([deviceString isEqualToString:@"iPhone5,3"])    return @"iPhone 5C";
    else if ([deviceString isEqualToString:@"iPhone5,4"])    return @"iPhone 5C";
    else if ([deviceString isEqualToString:@"iPhone6,1"])    return @"iPhone 5S";
    else if ([deviceString isEqualToString:@"iPhone6,2"])    return @"iPhone 5S";
    else if ([deviceString isEqualToString:@"iPhone7,1"])    return @"iPhone 6 Plus";
    else if ([deviceString isEqualToString:@"iPhone7,2"])    return @"iPhone 6";
    else if ([deviceString isEqualToString:@"iPhone8,1"])    return @"iPhone 6s";
    else if ([deviceString isEqualToString:@"iPhone8,2"])    return @"iPhone 6s Plus";
    else if ([deviceString isEqualToString:@"iPhone8,4"])    return @"iPhone SE";
    else if ([deviceString isEqualToString:@"iPhone9,1"])    return @"iPhone 7";
    else if ([deviceString isEqualToString:@"iPhone9,2"])    return @"iPhone 7 Plus";
    // iPod
    else if ([deviceString isEqualToString:@"iPod1,1"])   return @"iPod Touch 1G";
    else if ([deviceString isEqualToString:@"iPod2,1"])   return @"iPod Touch 2G";
    else if ([deviceString isEqualToString:@"iPod3,1"])   return @"iPod Touch 3G";
    else if ([deviceString isEqualToString:@"iPod4,1"])   return @"iPod Touch 4G";
    else if ([deviceString isEqualToString:@"iPod5,1"])   return @"iPod Touch 5G";
    // iPad
    else if ([deviceString isEqualToString:@"iPad1,1"])   return @"iPad 1G";
    else if ([deviceString isEqualToString:@"iPad2,1"])   return @"iPad 2";
    else if ([deviceString isEqualToString:@"iPad2,2"])   return @"iPad 2";
    else if ([deviceString isEqualToString:@"iPad2,3"])   return @"iPad 2";
    else if ([deviceString isEqualToString:@"iPad2,4"])   return @"iPad 2";
    else if ([deviceString isEqualToString:@"iPad2,5"])   return @"iPad Mini 1G";
    else if ([deviceString isEqualToString:@"iPad2,6"])   return @"iPad Mini 1G";
    else if ([deviceString isEqualToString:@"iPad2,7"])   return @"iPad Mini 1G";
    else if ([deviceString isEqualToString:@"iPad3,1"])   return @"iPad 3";
    else if ([deviceString isEqualToString:@"iPad3,2"])   return @"iPad 3";
    else if ([deviceString isEqualToString:@"iPad3,3"])   return @"iPad 3";
    else if ([deviceString isEqualToString:@"iPad3,4"])   return @"iPad 4";
    else if ([deviceString isEqualToString:@"iPad3,5"])   return @"iPad 4";
    else if ([deviceString isEqualToString:@"iPad3,6"])   return @"iPad 4";
    else if ([deviceString isEqualToString:@"iPad4,1"])   return @"iPad Air";
    else if ([deviceString isEqualToString:@"iPad4,2"])   return @"iPad Air";
    else if ([deviceString isEqualToString:@"iPad4,3"])   return @"iPad Air";
    else if ([deviceString isEqualToString:@"iPad4,4"])   return @"iPad Mini 2G";
    else if ([deviceString isEqualToString:@"iPad4,5"])   return @"iPad Mini 2G";
    else if ([deviceString isEqualToString:@"iPad4,6"])   return @"iPad Mini 2G";
    // iPhone Simulator
    else if ([deviceString isEqualToString:@"i386"])      return @"iPhone Simulator";
    else if ([deviceString isEqualToString:@"x86_64"])    return @"iPhone Simulator";
    else return deviceString;
}

+ (NSString *)getIP
{
    BOOL success;
    struct ifaddrs * addrs;
    const struct ifaddrs * cursor;
    success = getifaddrs(&addrs) == 0;
    if (success) {
        cursor = addrs;
        while (cursor != NULL) {
            // the second test keeps from picking up the loopback address
            if (cursor->ifa_addr->sa_family == AF_INET && (cursor->ifa_flags & IFF_LOOPBACK) == 0)
            {
                NSString *name = [NSString stringWithUTF8String:cursor->ifa_name];
                if ([name isEqualToString:@"en0"]) // Wi-Fi adapter
                    NSLog(@"IP:%@",[NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)cursor->ifa_addr)->sin_addr)]);
                return [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)cursor->ifa_addr)->sin_addr)];
            }
            cursor = cursor->ifa_next;
        }
        freeifaddrs(addrs);
    }
    return nil;
}

@end
